package com.qding.park.adapter.bsg.adapter.impl;

import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;

import com.alibaba.fastjson.JSONObject;
import com.qding.park.adapter.bsg.adapter.config.BSGConfig;
import com.qding.park.adapter.bsg.adapter.constant.ConstantFunction;
import com.qding.park.adapter.bsg.adapter.entity.ResponseMsg;
import com.qding.park.adapter.bsg.adapter.enums.ErrorCodeEnum;
import com.qding.park.adapter.bsg.adapter.util.ResponseParser;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.network.HttpUtil;
import com.qding.park.manager.pojo.ap.ApParkTypeInfo;
import com.qding.park.target.common.adapter.RuleAdapter;

/**
 * @ClassName RuleAdapterImpl
 * @Description TODO(规则适配器实现)
 * @author Administrator
 * @Date 2016年8月28日 下午9:45:34
 * @version 1.0.0
 */
public class RuleAdapterImpl  implements RuleAdapter {
	
	private static final Log _log = LogFactory.getLog(RuleAdapterImpl.class);
	
	public RuleAdapterImpl(String url){
//		this.setAdapterUrl(url);
	}
	
	public RuleAdapterImpl(){
    }

	@Override
	public List<ApParkTypeInfo> AccountRuleByParkId(String parkCode) throws AdapterException {
		// TODO Auto-generated method stub
		
	    String url = BSGConfig.getAdapterUrl();
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("clientID", parkCode);
		Map<String, String> params = new HashMap<String, String>();
		params.put("UserID", "110");
		params.put("RequestFun", ConstantFunction.Parking_ParkType);
		params.put("RequestData", jsonObj.toJSONString());
		params.put("Sign", "ddd");
		try {
			_log.info("adapter request:"+params);
			String result = HttpUtil.doPost(url, params);
			_log.info("adapter response:"+result);
			java.io.Reader in = new StringReader(result);
			Document doc=doc = (new SAXBuilder()).build(in);

			String temp = doc.getRootElement().getValue();
			in.close();
			ResponseMsg repmsg = JSONObject.parseObject(temp, ResponseMsg.class);
			String errorCode = repmsg.getErrorCode();
			JSONObject data=(JSONObject) repmsg.getResult();
			if (!ErrorCodeEnum.SUCCESS.getCode().equals(errorCode)) {
				throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
						ExceptionMessage.createExceptionMessage(
								AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), repmsg.getErrorMsg(),
								ConstantFunction.Parking_ParkType + " adpater处理出错: "+repmsg.getErrorMsg()+",parkCode="+parkCode));
			}
			String text=data.getString("data");
			List<ApParkTypeInfo> resultInfos=ResponseParser.parking_parkType_parser(text);
			return resultInfos;
//			String xmlData=(String) repmsg.getResult();
//			 Document doc=DocumentHelper.parseText(xmlData);
//			 Element root =doc.getRootElement();
//			JSONObject data = (JSONObject) repmsg.getResult();
//			ApUserCarInfo apUserCarInfo = new ApUserCarInfo();
//			apUserCarInfo.setIsCharge(data.getString("isCharge"));
//			apUserCarInfo.setExpireDate(DateUtils.parseDate(data.getString("expireDate"), DateUtils.DATE_SHORT_DATEONLY));
//			return apUserCarInfo;
		} catch (AdapterException e) {
			throw e;
		}  catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
}
